package com.example.apptest.fragment;

import static java.lang.Math.sin;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.example.app_test.R;
import com.example.apptest.GraphThread;
import com.example.apptest.events.EventSpinMove;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MySurface extends SurfaceView implements SurfaceHolder.Callback {

    private Paint paint;
    private Bitmap img;

    private Matrix matrix;

    private double rotatingSpeed = 0.0;
    private double movingSpeed = 0.0;
    private float x = 0f;
    private float y = 0f;

    private float degrees = 0.0f;

    private GraphThread thread;

    private int width,height;

    //加载飞机图像资源
    //设置 SurfaceHolder 回调，监听 Surface 的创建、改变和销毁
    //注册 EventBus 以接收外部事件
    public MySurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        img = BitmapFactory.decodeResource(getResources(), R.drawable.plane);
        getHolder().addCallback(this);
        setWillNotDraw(false);
        matrix = new Matrix();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        degrees += rotatingSpeed;
        if(degrees > 360f) {
            degrees = 0f;
        }

        x += movingSpeed * Math.sin(Math.toRadians(degrees));
        y -= movingSpeed * Math.cos(Math.toRadians(degrees));

        if(y<0) {
            y=0;
        }
        if(y>height-img.getHeight()/2) {
            y=height-img.getHeight()/2;
        }
        if(x<0) {
            x=0;
        }
        if(x>width-img.getWidth()/2) {
            x=width-img.getWidth()/2;
        }
        matrix.setTranslate(0,0);
        matrix.setRotate(degrees, img.getWidth()/2, img.getHeight()/2);
        matrix.postTranslate(x, y);
        canvas.drawBitmap(img, matrix, paint);
    }

    //在 Surface 创建时获取视图尺寸，用于后续边界计算
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        width = getWidth();
        height = getHeight();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

    }

    //提供公共方法控制飞机的旋转和移动状态
    public void startRotating() {
        rotatingSpeed = 5.0;
    }

    public void stopRotating() {
        rotatingSpeed = 0.0;
    }

    public void startMoving() {
        movingSpeed = 5.0;
    }

    public void stopMoving() {
        movingSpeed = 0.0;
    }

    public void stopGraph() {
        rotatingSpeed = 0.0;
        movingSpeed = 0.0;
        thread.setPause();
    }

    public void runGraph() {
        thread = new GraphThread(this);
        thread.setRun();
        thread.start();
    }


    //使用 EventBus 接收外部事件，动态调整飞机的旋转和移动速度
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSpinMove(EventSpinMove event) {
        movingSpeed = event.moveSpeed;
        rotatingSpeed = event.spinSpeed;

//        this.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                movingSpeed = 0.0;
//                rotatingSpeed = 0.0;
//            }
//        }, 20);
    }
}